package com.dura.api.filter.impl;

import com.dura.api.client.BeaconCacheClient;
import com.dura.common.enums.ExceptionEnums;
import com.dura.common.exception.ApiException;
import com.dura.api.filter.CheckFilter;
import com.dura.common.constant.CacheConstant;
import com.dura.common.model.StandardSubmit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 检测客户的apiKey 合法
 * @version 1.0
 * @Author 振鹏
 * @Date 2025/3/15 9:47
 * @注释
 */
@Service(value = "apikey")
@Slf4j
public class ApiKeyCheckFilter implements CheckFilter {

    @Autowired
    private BeaconCacheClient cacheClient;

    @Override
    public void check(StandardSubmit submit) {
        log.info("【接口模块-校验apikey】   校验ing…………");
        // 开始书写关于apikey 的校验方案
        Map clientBusiness = cacheClient.hGetAll(CacheConstant.CLIENT_BUSINESS + submit.getApikey());

        // 如果null，不存在客户信息
        if(null==clientBusiness || clientBusiness.size()==0){
            log.info("【接口模块-校验apikey】 非法key"+submit.getApikey());
            throw new ApiException(ExceptionEnums.ERROR_APIKEY);
        }

        // 数据存在，正常封装数据
        submit.setClientId(Long.parseLong(clientBusiness.get("id")+""));
        log.info("【接口模块-校验apikey】 查询到客户信息 clientBusiness = {}",clientBusiness);
    }
}
